package com.lishang.location.helper.plugin

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.graphics.Outline
import android.os.Bundle
import android.util.Log
import android.view.View
import android.view.ViewOutlineProvider
import android.widget.Button
import android.widget.RelativeLayout
import android.widget.Toast
import com.lishang.location.helper.ui.SettingActivity
import com.lishang.location.helper.utils.ResourceUtil
import de.robv.android.xposed.XC_MethodHook

class SettingDetailActivityHook : XC_MethodHook() {
    init {
        Log.e("ActivityHook", "SettingDetailActivityHook init")
    }

    override fun afterHookedMethod(param: MethodHookParam) {
        Log.e("ActivityHook", "afterHookedMethod ")
        val activity = param.thisObject as Activity

        activity.findViewById<View>(ResourceUtil.getId(activity, "h95")).let {
            it.parent as RelativeLayout
        }.addView(
            createView(activity)
        )
    }

    private fun createView(context: Context) = Button(context).apply {
        val size = context.resources.getDimensionPixelSize(ResourceUtil.getDimenId(context, "ot"))
        Log.e("ActivityHook", "afterHookedMethod size:$size")
        layoutParams = RelativeLayout.LayoutParams(size, size).apply {
            addRule(
                RelativeLayout.ALIGN_PARENT_RIGHT
            )
            addRule(
                RelativeLayout.CENTER_VERTICAL
            )
        }

        text = "打卡\n助手"
        textSize = 12f
        setTextColor(Color.WHITE)
        setBackgroundColor(0x80000000.toInt())
        outlineProvider = object : ViewOutlineProvider() {
            override fun getOutline(view: View, outline: Outline) {
                outline.setRoundRect(
                    0,
                    0,
                    view.width,
                    view.height,
                    view.width / 2f
                )
            }
        }
        clipToOutline = true

        setOnClickListener {
            context.startActivity(Intent(Intent.ACTION_VIEW).apply {
                setClassName("com.lishang.location.helper", SettingActivity::class.java.name)
            })
        }

    }

}